package com.jd.order.service.impl;

import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.basic.util.PageList;
import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.mapper.OrderPetAcquisitionMapper;
import com.jd.order.query.OrderPetAcquisitionQuery;
import com.jd.order.service.IOrderPetAcquisitionService;
import com.jd.org.domain.Employee;
import com.jd.org.domain.Shop;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.org.mapper.ShopMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.dto.OrderDto;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import com.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        // 判断登陆人是平台管理员还是店铺管理员还是店员
        // 获取登陆人信息
        Logininfo loginfo = LoginContext.getLoginfo(request);
        Employee employee = employeeMapper.findByLogininfoId(loginfo.getId());
        if (employee.getShopId() != null){ // 店铺或者店员
            query.setShopId(employee.getShopId());
            Shop shop = shopMapper.findOne(employee.getShopId());
            if (!shop.getAdminId().equals(employee.getId())){ // 店员
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public AjaxResult cancel(Long orderId) {
        // 取消订单放入寻主池里
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderId);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId()); // 拒单
        // 修改订单状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        return new AjaxResult();
    }

    @Override
    public AjaxResult confirm(OrderDto orderDto) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        // 生成宠物对象
        Pet pet = msgOrderDto2Pet(msg,orderDto);
        petMapper.add(pet);
        // 修改订单状态
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPetId(orderDto.getId());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);
        // 修改寻主消息
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        return new AjaxResult();
    }

    private Pet msgOrderDto2Pet(SearchMasterMsg msg, OrderDto orderDto) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(orderDto.getMoney());
       //pet.setSaleprice(); // 上架价格
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
//        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
